/*********************************************************************************************************
* Movement in form of a cursor. Follow and reaching one destination point
*
*  inspired by http://www.freeactionscript.com/2010/09/player-movement-click-to-move/
*
*  Author of the ported code: Florian Weil - www.derhess.de
*
************************************************************************************************************/

inlets = 2;
outlets = 2;

// distance between two points or coordinate
var velocity = 5;

var canvas_width = 640;
var canvas_height = 480;

var x_pos = canvas_width/2;
var y_pos = canvas_height/2;
var x_dest = x_pos;
var y_dest = y_pos;

function setDestinationPoint(x_target,y_target)
{
	x_dest = x_target;
	y_dest = y_target;
}

function setVelocity(value)
{
	if(value > 0)
		velocity = value;
	else
		post("ERROR: velocity value is negative. It must be greater than 0!\n");
}



function move() 
{
	x_pos += (x_dest - x_pos) / velocity;
	y_pos += (y_dest - y_pos) / velocity;
}

function reset()
{
	velocity = 5;
	x_pos = canvas_width/2;
	y_pos = canvas_height/2;
}

//////////////////////////////////////////////////////////////////////////
//    MAX MSP FUNCTION
//////////////////////////////////////////////////////////////////////////
function bang()
{
	move();
	output();
}

//////////////////////////////////////////////////////////////////////////
//    Helper Functions
//////////////////////////////////////////////////////////////////////////
function output()
{
	outlet(0, x_pos);
    outlet(1, y_pos);
	//post("velocityToPoint: Send Bang: " + x_pos + "/" + y_pos + "\n");
}
